package com.github.niefy.modules.wx.controller;

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonToken;
import com.github.niefy.common.utils.Json;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.sys.entity.SysUserEntity;
import com.github.niefy.modules.wx.entity.WxMpTemplateDataEx;
import com.github.niefy.modules.wx.entity.WxTemplateLog;
import com.github.niefy.modules.wx.entity.WxTemplateLogEntity;
import com.github.niefy.modules.wx.form.TemplateMsgBatchForm;
import com.github.niefy.modules.wx.service.WxTemplateLogService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-11-18 10:39:26
 */
@RestController
@RequestMapping("wx/wxtemplatelog")
public class WxTemplateLogController {
    @Autowired
    private WxTemplateLogService wxTemplateLogService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("wx:wxtemplatelog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wxTemplateLogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("wx:wxtemplatelog:info")
    public R info(@PathVariable("id") Long id){
		WxTemplateLogEntity wxTemplateLog = wxTemplateLogService.getById(id);

        return R.ok().put("wxTemplateLog", wxTemplateLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("wx:wxtemplatelog:save")
    public R save(@CookieValue String appid, @RequestBody WxTemplateLog wxTemplateLog){
        this.wxMpService.switchoverTo(appid);

        WxTemplateLogEntity wxTemplateLogEntity = new WxTemplateLogEntity();
        wxTemplateLogEntity.setAppid(appid);
        wxTemplateLogEntity.setTagid(wxTemplateLog.getTagid());
        wxTemplateLogEntity.setTagName(wxTemplateLog.getTagName());
        wxTemplateLogEntity.setAddressId(wxTemplateLog.getAddressId().toString());
        wxTemplateLogEntity.setAddressName(wxTemplateLog.getAddressName());
        wxTemplateLogEntity.setTemplateId(wxTemplateLog.getTemplateId());
        wxTemplateLogEntity.setTemplateName(wxTemplateLog.getTemplateName());
        wxTemplateLogEntity.setData(wxTemplateLog.getData().toString());
        wxTemplateLogEntity.setSendData(wxTemplateLog.getSendData());
        wxTemplateLogEntity.setDataName(wxTemplateLog.getDataName());
        wxTemplateLogEntity.setUrl(wxTemplateLog.getUrl());
        wxTemplateLogEntity.setMiniprogram(wxTemplateLog.getMiniprogram().toString());
        wxTemplateLogEntity.setSendDate(new Date());
        wxTemplateLogEntity.setSender(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());

        TemplateMsgBatchForm form = new TemplateMsgBatchForm();
        Map<String, Object> map = new HashMap<>(4);
        map.put("tagid",wxTemplateLog.getTagid());
        List<String> list = (List<String>) wxTemplateLog.getAddressId();
        for(int i = 0 ;i<list.size();i++){
            if(i==0&& list.get(i) !=null)
                map.put("communityId",list.get(i));
            if(i==1&& list.get(i) !=null)
                map.put("buildingNoId",list.get(i));
            if(i==2&& list.get(i) !=null)
                map.put("unitId",list.get(i));
        }

        form.setWxUserFilterParams(map);
        form.setUrl(wxTemplateLog.getUrl());
        form.setTemplateId(wxTemplateLog.getTemplateId());
        form.setMiniprogram(wxTemplateLog.getMiniprogram());
        List<WxMpTemplateData> WxMpTemplateDataList = new ArrayList<>();
        for (WxMpTemplateDataEx wxMpTemplateDataEx:
        wxTemplateLog.getData()) {
            WxMpTemplateDataList.add(new WxMpTemplateData(wxMpTemplateDataEx.getName(),wxMpTemplateDataEx.getValue(),wxMpTemplateDataEx.getColor()));
        }
        form.setData(WxMpTemplateDataList);
        wxTemplateLogService.sendMsgBatch(form,appid);
		wxTemplateLogService.save(wxTemplateLogEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("wx:wxtemplatelog:update")
    public R update(@RequestBody WxTemplateLogEntity wxTemplateLog){
		wxTemplateLogService.updateById(wxTemplateLog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("wx:wxtemplatelog:delete")
    public R delete(@RequestBody Long[] ids){
		wxTemplateLogService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
